﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNet.Data;
using DotNet.Model;
using System.Data;
using DotNet.Common;
namespace DotNet.Business
{
    /// <summary>
    /// 新闻数量缓存 countByClass
    /// </summary>
    public class NewsBll
    {
        NewsDal nDal = new NewsDal();
        #region 是否存在该记录
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int newId)
        {
            return nDal.Exists(newId);
        }
        #endregion

        #region 获得新闻列表
        /// <summary>
        /// 获得新闻列表
        /// </summary>
        /// <returns></returns>
        public IList<News> GetModel()
        {
            return nDal.GetModel();
        }
        #endregion

        #region 根据新闻号查询实体
        /// <summary>
        /// 根据新闻号查询实体
        /// </summary>
        /// <param name="newId"></param>
        /// <returns></returns>
        public News GetModel(int newId)
        {
            return nDal.GetModel(newId);
        }
        #endregion

        #region 修改新闻信息
        /// <summary>
        /// 修改新闻信息
        /// </summary>
        /// <param name="news"></param>
        /// <returns></returns>
        public bool Update(News news)
        {
            return nDal.Update(news);
        }
        #endregion

        #region 删除新闻根据新闻号
        /// <summary>
        /// 删除新闻根据新闻号
        /// </summary>
        /// <param name="newId"></param>
        /// <returns></returns>
        public bool Delete(int newId)
        {
            CacheUtil.RemoveByRegexp(@"News_.?");
            return nDal.Delete(newId);
        }
        #endregion

        #region 添加新闻
        /// <summary>
        /// 添加新闻
        /// </summary>
        /// <param name="news"></param>
        /// <returns></returns>
        public bool Insert(News news)
        {
            CacheUtil.RemoveByRegexp(@"News_.?");
            return nDal.Insert(news);
        }
        #endregion

        #region 新闻分页---郑同飞
                #region 新闻分页---新闻id，新闻关键字     
                /// <summary>
                /// 新闻分页------新闻id，新闻关键字   
                /// </summary>
                /// <param name="pageIndex"></param>
                /// <param name="pageSize"></param>
                /// <param name="classid"></param>
                /// <param name="key">搜索的关键字，可为空值（为空则用nul）</param>
                /// <returns></returns>
                public DataTable pageingForNews(int pageIndex, int pageSize, int classid,string key) 
                {
                    if (classid==0&&key=="")
                    {
                        return nDal.pageingForNews(pageIndex, pageSize);
                    }
                    else if(classid==0&&key!="")
                    {
                        return nDal.pageingForNews(pageIndex, pageSize, key);
                    }
                    else if(classid>0&&key=="")
                    {
                        return nDal.pageingForNews(pageIndex, pageSize, classid);
                    }
                    else
                    return nDal.pageingForNews(pageIndex, pageSize, classid,key);
                }
                #endregion
        #endregion

        #region 获得新闻数，用于分页或者其它的地方---郑同飞
        /// <summary>
        ///获得新闻数，用于分页或者其它的地方---郑同飞
        /// </summary>
        /// <param name="classid"></param>
        /// <param name="where">获取新闻的条件，可为空（为空则用空字符串表示）</param>
        /// <returns></returns>
        public int getRecordCount(int classid,string where)
        {
            if (classid == 0 && where == "")
            {
                return nDal.getRecordCount();
            }
            else if (classid > 0 && where == "")
            {
                return nDal.getRecordCount(classid);
            }
            else if (classid == 0 && where != "")
            {
                return nDal.getRecordCount(where);
            }
            else
                return nDal.getRecordCount(classid,where);
            
        }
        #endregion

        #region 根据新闻编号批量删除新闻---郑同飞
        /// <summary>
        /// 根据新闻编号批量删除新闻---郑同飞
        /// </summary>
        /// <param name="nid"></param>
        /// <returns></returns>
        public bool Delete(string[] nid) 
        {
            CacheUtil.RemoveByRegexp(@"News_.?");
            return nDal.Delete(nid);
        }
        #endregion

        #region 批量移动新闻--郑同飞
        /// <summary>
        /// 批量移动新闻--郑同飞
        /// </summary>
        /// <param name="nid"></param>
        /// <param name="classId"></param>
        /// <returns></returns>
        public bool Update(string[] nid,int classId) 
        {
            CacheUtil.RemoveByRegexp(@"News_.?");
            return nDal.Update(nid, classId);
        }
        #endregion

        #region 查询每个类别下面的新闻数量根据类别号--郑同飞
        /// <summary>
        /// 查询每个类别下面的新闻数量根据类别号--郑同飞
        /// </summary>
        /// <param name="ncid">新闻类别</param>
        /// <returns></returns>
        public int countByClass(int ncid)
        {
            string CKey = "News_" + ncid;
            if (!CacheUtil.IsExist(CKey))
            {
                CacheUtil.Insert(CKey,nDal.countByClass(ncid),60);
            }
            return Convert.ToInt32(CacheUtil.Read(CKey));
        }
        #endregion
    }
}
